
import java.io.File;

/**
 * 
 * @author bruno
 *
 */
public class Mirror {
	
	// Paths to the directories to be tracked
	private String path1;
	private String path2;
	
	// File objects to the directories 
	private File dir1;
	private File dir2;
	
	// Update time period
	private int updateTime;
	
	// 
	private String killAgent;
	
	/**
	 * Constructor
	 * @param path1
	 * @param path2
	 */
	public Mirror(String path1, String path2, int updateTime, String killAgent){
		this.path1 = path1;
		this.path2 = path2;
		this.updateTime = updateTime;
		this.killAgent = killAgent;
		
		this.dir1 = new File(path1);
		this.dir2 = new File(path2);
		
		// Verifies arguments
		this.verifyArguments();
		
		// START TRACKING/MONITORING
//		this.startProgram();
	}
	
	/**
	 * Verifies if paths informed in the arguments corresponds to directories
	 */
	private void verifyArguments() {
		
		if (!dir1.isDirectory()) {
			System.err.println("============================================");
			System.err.println("The 1st argument "+this.dir1+" is not a directory!\n");
			System.err.println("============================================");
			System.exit(-1);
		}

		if (!dir2.isDirectory()) {
			System.err.println("============================================");
			System.err.println("The 2nd argument "+this.dir2+" is not a directory!\n");
			System.err.println("============================================");
			System.exit(-1);
		}
		
	}

	/**
	 * Starts monitoring/tracking directories 
	 */
	public void startProgram() {
		
		do {
			// Makes the program sleep 
			try { Thread.sleep(updateTime*1000); } 
			catch(InterruptedException e) { e.printStackTrace(); }
			
			System.out.println("============================================");
			System.out.println("    PASSOU NO LACO!   ");
			System.out.println("============================================");
			
			this.analiseDirectories();
			
			
		} while(! this.stopMonitoring());
		
	}
	
	/**
	 * Looks for a file named specifically to end the program execution
	 * @return
	 */
	private boolean stopMonitoring() {
            String[] files;

            files = dir1.list();
            for (String f: files) {
                    if (killAgent.equalsIgnoreCase(f))
                            return true;
            }

            files = dir2.list();
            for (String f: files) {
                    if (killAgent.equalsIgnoreCase(f))
                            return true;
            }

            return false;
	}
	
	
	/**
	 * Compares the 2 directories looking for differences between them
	 * 
	 *  Map the directories to a tree data structure
	 */
	private void analiseDirectories(){
		
//		String folderPath = this.path1;
            FolderTraversar traversal = new FolderTraversar(dir1);
            traversal.traverse();
		
	}
	
	// Copiar do 1 para 2, ou do 2 para 1 ???

		
	//=================================
	// Getters and setters
	//=================================
	public void setPath1(String path1) {
		this.path1 = path1;
	}

	public String getPath1() {
		return path1;
	}

	public void setPath2(String path2) {
		this.path2 = path2;
	}

	public String getPath2() {
		return path2;
	}

}
